/**
 * @class refrigeratory.pn_west
 * @extends Ext.Panel
 *
 * refrigeratory.pn_west description
 *
 * @author    Maple
 * @copyright (c) 2009, refrigeratory
 * @date      9. February 2009
 * @version   0.1
 * @revision  0.1
 *
 */
 
// create namespace
Ext.ns('refrigeratory');
refrigeratory.SITE_URL = '';

/**
 * Creates new refrigeratory.pn_west
 * @constructor
 * @param {Object} config A config object
 */
refrigeratory.pn_west = Ext.extend(Ext.Panel, {
    region:'west',
    id:'pn_west',
    split:true,
    width: 200,
    minSize: 175,
    maxSize: 400,
    margins:'0 0 0 0',
    layout:'accordion',
    layoutConfig:{
        animate:true
    }
    // {{{
    ,initComponent:function() {
 
        // hard coded config (it cannot be changed while instantiating)
        // {{{
        var config = {
            title: this.titleText,
            // collapsible放着上面会引起标题行不会收缩
            collapsible: true,
        }; // eo config object
 
        // apply config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        // }}}
 
        // call parent
        refrigeratory.pn_west.superclass.initComponent.apply(this, arguments);	
 
        // parent call post-processing, e.g. install event handlers
		
    } // eo function initComponent
    // }}}
    // {{{
    ,onRender:function() {
 
        // parent call pre-processing
 
        // call parent
        refrigeratory.pn_west.superclass.onRender.apply(this, arguments);
		this.createMenu();
        // parent call post-processing, e.g. install event handlers on rendered components
		
    } // eo function onRender
    // }}}
    
    // 需被本地化的文本
    ,titleText: '导航菜单'
    // private 生成相关导航菜单
    ,createMenu : function(){	
		var requestConfig ={  
			url:'get.php'
			,params:{
    			action:'menu'
    			,operation: 'getPanels'
    		}
			,scope:this
			,callback:function(options,success,response){  
				treejsonData = Ext.util.JSON.decode(response.responseText);
					for(var i=0;i<treejsonData.count;i++){  
						refrigeratory.pn_west.superclass.add.call(this,this.genMenuPanel(treejsonData.data[i]));  
					}
				this.doLayout();				
			}   
		}  
		Ext.Ajax.request(requestConfig); 		
	}
	//生成一级菜单panel
	,genMenuPanel:function(jsonData){
			var root = new Ext.tree.TreeNode('root');
			var trpMenu = new Ext.tree.TreePanel({
				id: jsonData.pn_id,
				title: jsonData.name,
				autoScroll: true,
				iconCls:'refrigeratory-icon-education',
				expanded: false,
				singleExpand: false,
				useArrows: true,
				rootVisible: false,
				root: root
			});
			trpMenu.on('click', this.onTreeNodeClick.createDelegate(trpMenu));
			this.genMenuNodes(jsonData,root,this);	
			return 	trpMenu;				
	}
	//生成二级以下菜单节点
	,genMenuNodes:function(jsonData,root,scope)
	{			
		var requestConfig ={  
			url:'get.php'
			,params:{
				action:'menu'
				,operation: 'getNodes'
				,parentid:jsonData.id_menu
			}
			,callback:function(options,success,response){  
				nodeDate = Ext.util.JSON.decode(response.responseText);
				for(var i=0;i<nodeDate.count;i++){ 
					initdata = nodeDate.data[i];
					if(initdata.isleaf==0){
						var node = new Ext.tree.TreeNode(initdata.name);
						root.appendChild(node);	
						scope.genMenuNodes(initdata,node,scope);	
					}else{
						var node = new Ext.tree.TreeNode({
							id:initdata.pn_id
							,text: initdata.name
							,leaf:true
							});
						root.appendChild(node);	
					}		
				}		
			}   
		} 		
		Ext.Ajax.request(requestConfig); 	
		
	}
	
    ,onTreeNodeClick: function(n, e){
        var sn = this.selModel.selNode || {};     // 初识时 selNode 为 null
        // 此处是因为选择某一模块，但关闭后又选择该模块会被忽略的问题
        if(!n.leaf){   
			return;
		}		// 选择非节点或选择项为当前项则忽略
        if (n.id == "tn_chgpassword"){
        	if (!Ext.get("win_changepassword"))
        		(new refrigeratory.win_changepassword()).show();
			else
				Ext.get("win_changepassword").show();
        }
		
        else{
            var pn_id = n.id.replace("tn_", "pn_");
            var tbp_main = Ext.getCmp('tbp_main');
            if (tbp_main.findById(pn_id))
                tbp_main.findById(pn_id).show();
            else
                tbp_main.add(eval('new refrigeratory.'+pn_id+'()')).show();
        }
        //}    
    }
}); // eo extend
 
// register xtype
Ext.reg('pn_west', refrigeratory.pn_west); 
// eof